﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace Demo
{
    public partial class MyGridView : UserControl
    {
        private string connectionString;
        public string ConnectionString
        {
            get
            {
                return connectionString;
            }
            set
            {
                connectionString = value;
            }
        }

        private string tableName = "";
        public string TableName
        {
            get
            {
                return tableName;
            }
            set
            {
                tableName = value;
            }
        }

        public MyGridView()
        {
            InitializeComponent();

            
        }

        private void MyGridView_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.connectionString) || string.IsNullOrEmpty(this.tableName))
                return;
            
            using (SqlConnection sqlConnection = new SqlConnection())
            {
                sqlConnection.ConnectionString = this.connectionString;
                sqlConnection.Open();
                
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(
                    "SELECT * FROM " + this.tableName, sqlConnection);
                DataTable dataTable = new DataTable();

                sqlDataAdapter.Fill(dataTable);
                dataGridView1.DataSource = dataTable;
                
                sqlConnection.Close();
            }
        }
    }
}
